package sentinel;


//漏桶算法：模拟sentinel
//优点：匀速 少占用空间
public class Class03_LeakyBucket_02 {
    public long timeStemp = System.currentTimeMillis();
    public int wantTime = 1;//预期qps 1s
    public int wantQuest = 10;//10个请求
    public int maxTime = 1000;//能接受的最大超时时间

    public boolean limit() throws InterruptedException {
        long now = System.currentTimeMillis();
        int rate = wantTime * 1000 / wantQuest;//期望间隔ms
        if(now - rate > maxTime){
            return false;
        }
        int wait = (int) (timeStemp + rate  - now);
        timeStemp = now;
        if(wait <= 0){
            return true;
        }else {
            Thread.sleep(wait);
            return true;
        }
    }
}
